﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _templateedit : JumbotCms.API.AdminCenter
    {
        public string tpPath = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Admin_Load("0000", "html");
            id = Str2Str(q("id"));
            string pid = Str2Str(q("pid"));

            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", pid);
            tpPath = doh.GetValue("jcms_normal_templateproject", "Dir").ToString();
            if (tpPath.Length == 0)
            {
                Response.Write("HTML模板方案选择有误!");
                Response.End();
                return;
            }
            if (!Page.IsPostBack)
            {
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[Title] FROM [jcms_normal_skins] ORDER BY Id asc";
                DataTable dtSkins = doh.GetDataTable();
                this.ddlSkinId.DataSource = dtSkins;
                this.ddlSkinId.DataTextField = "Title";
                this.ddlSkinId.DataValueField = "ID";
                this.ddlSkinId.DataBind();
                dtSkins.Clear();
                dtSkins.Dispose();
            }
            Jumbot.DataOper.WebFormHandler wh = new Jumbot.DataOper.WebFormHandler(doh, "jcms_normal_template", btnSave);
            wh.AddBind(txtTitle, "Title", true);
            wh.AddBind(txtSource, "Source", true);
            wh.AddBind(ddlSkinId, "SkinId", false);
            if (id == "0")
            {
                string isDef = "0";
                wh.AddBind(ref isDef, "IsDefault", false);
                wh.Mode = Jumbot.DataOper.OperationType.Add;
            }
            else
            {
                wh.ConditionExpress = "id=" + id;
                this.txtSource.Enabled = false;
                wh.Mode = Jumbot.DataOper.OperationType.Modify;
            }
            wh.validator = chkForm;
            wh.AddOk += new EventHandler(save_ok);
            wh.ModifyOk += new EventHandler(save_ok);
        }
        protected void bind_ok(object sender, EventArgs e)
        {
        }
        protected bool chkForm()
        {
            if (!CheckFormUrl())
                return false;
            if (!Page.IsValid)
                return false;
            doh.Reset();
            doh.ConditionExpress = "Title=@title and id<>" + id;
            doh.AddConditionParameter("@title", txtTitle.Text);
            if (doh.Exist("jcms_normal_template"))
            {
                FinalMessage("模板名重复!", "", 1);
                return false;
            }
            return true;
        }
        protected void save_ok(object sender, EventArgs e)
        {
            if (id == "0")
            {
                Jumbot.DataOper.DbOperEventArgs de = (Jumbot.DataOper.DbOperEventArgs)e;
                id = de.id.ToString();

                doh.Reset();
                doh.ConditionExpress = "id=@id";
                doh.AddConditionParameter("@id", id);
                doh.AddFieldItem("type", q("type"));
                doh.AddFieldItem("stype", q("stype"));
                doh.AddFieldItem("pId", q("pid"));
                doh.Update("jcms_normal_template");
            }
            FinalMessage("成功保存", site.Dir + "admin/close.htm", 0);
        }
    }
}
